package manager_service.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manager_mapper.mapper.ChapterMapper;
import manager_service.service.BackChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test_common.entity.Chapter;
import test_common.entity.ChapterExample;

import java.util.Date;
import java.util.List;

/**
 * @author zhangzheng
 * @create 2021/1/5
 */
@Service
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public class BackChapterServiceImpl implements BackChapterService {
    @Autowired
    private ChapterMapper chapterMapper;

    @Override
    public void addChapter(Chapter chapter) {
        chapterMapper.insertSelective(chapter);
    }

    @Override
    public PageInfo<Chapter> showChapters(Integer pageNo, Integer pageSize,Integer courseId) {
        PageHelper.startPage(pageNo,pageSize);
        ChapterExample example=new ChapterExample();
        example.or()
                .andCourseIdEqualTo(courseId);
        List<Chapter> list=chapterMapper.selectByExample(example);
        PageInfo<Chapter> pageChapters=new PageInfo<>(list);
        return pageChapters;
    }

    @Override
    public void modifyStatus(Chapter chapter) {

        chapterMapper.updateByPrimaryKeySelective(chapter);
    }

    @Override
    public Chapter selectChapter(Integer chapterId) {
        Chapter chapter=chapterMapper.selectByPrimaryKey(chapterId);
        return chapter;
    }

    @Override
    public void modifyChapter(Chapter chapter) {
        chapterMapper.updateByPrimaryKeySelective(chapter);
    }

    @Override
    public PageInfo<Chapter> searchChapters(String title, String status, String id,String courseId, Date dateStart, Date dateEnd,Integer pageNoint, Integer pageSizeint) {
        PageHelper.startPage(pageNoint,pageSizeint);
        List<Chapter> list=chapterMapper.searchChapters(title, status, id, courseId,dateStart, dateEnd);
        PageInfo<Chapter> chapterPageInfo=new PageInfo<>(list);
        return chapterPageInfo;
    }
}
